package com.crm.incoming.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.crm.incoming.domain.CustIncomingInfo;
import com.crm.incoming.service.ICustIncomingInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的审件Controller
 *
 * @author ruoyi
 * @date 2024-02-03
 */
@RestController
@RequestMapping("/incoming/incoming")
public class CustIncomingInfoController extends BaseController {
    @Autowired
    private ICustIncomingInfoService custIncomingInfoService;

    /**
     * 查询我的审件列表
     */
    @PreAuthorize("@ss.hasPermi('incoming:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustIncomingInfo custIncomingInfo) {
        if (custIncomingInfo.getStatus() == null) {
            custIncomingInfo.setStatus("1");
        }
        custIncomingInfo.setEmpId(getLoginUser().getUserId());
        Map<String, Object> params = custIncomingInfo.getParams();
        String timeContent = MapUtils.getString(params, "timeContent", "");
        String timeType = MapUtils.getString(params, "timeType", "");
        if (!Objects.equals(timeContent, "") && !Objects.equals(timeType, "")) {
            String[] times = timeContent.split(",");
            params.put(timeType + "start", times[0]);
            params.put(timeType + "end", times[1]);
            custIncomingInfo.setParams(params);
        }
        startPage();
        List<CustIncomingInfo> list = custIncomingInfoService.selectCustIncomingInfoList(custIncomingInfo);
        return getDataTable(list);
    }

    /**
     * 导出我的审件列表
     */
    @PreAuthorize("@ss.hasPermi('incoming:export')")
    @Log(title = "我的审件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustIncomingInfo custIncomingInfo) {
        List<CustIncomingInfo> list = custIncomingInfoService.selectCustIncomingInfoList(custIncomingInfo);
        ExcelUtil<CustIncomingInfo> util = new ExcelUtil<CustIncomingInfo>(CustIncomingInfo.class);
        util.exportExcel(response, list, "我的审件数据");
    }

    /**
     * 获取我的审件详细信息
     */
    @PreAuthorize("@ss.hasPermi('incoming:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(custIncomingInfoService.selectCustIncomingInfoById(id));
    }

    /**
     * 新增我的审件
     */
    @PreAuthorize("@ss.hasPermi('incoming:add')")
    @Log(title = "我的审件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustIncomingInfo custIncomingInfo) {
        return toAjax(custIncomingInfoService.insertCustIncomingInfo(custIncomingInfo));
    }

    /**
     * 修改我的审件
     */
    @PreAuthorize("@ss.hasPermi('incoming:edit')")
    @Log(title = "我的审件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustIncomingInfo custIncomingInfo) {
        return toAjax(custIncomingInfoService.updateCustIncomingInfo(custIncomingInfo));
    }

    /**
     * 删除我的审件
     */
    @PreAuthorize("@ss.hasPermi('incoming:remove')")
    @Log(title = "我的审件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(custIncomingInfoService.deleteCustIncomingInfoByIds(ids));
    }
}
